<?
function getlistMenu($name,$selected="",$type="")
{
	$q = query("select * from menu order by sort asc");
	while($r=fetch($q))
	{
		$menu[$r["parent"]][]=$r;
	}
	$ret="<link href='./admin/libs/select2/select2.css' rel='stylesheet'/>
			<script src='./admin/libs/select2/jquery-1.7.1.min.js'></script>
			 <script src='./admin/libs/select2/select2.js'></script>	<select name='$name' id='selectmn' style='width:200px;'>";
	$ret.="<option value=0 data-level='0' ".($type!=""?"disabled=''":"").">Root</option>\n";
	if(count($menu[0])>0)
	foreach($menu[0] as $mn)
	{
		$ret.="<option value=$mn[id] data-level='0' ".($mn["id"]==$selected?"selected=''":"")." ".($type!="" && !strpos("a".$mn["type"],$type)?"disabled=''":"").">$mn[name]</option>\n";
		if(count($menu[$mn["id"]])>0)
		foreach($menu[$mn["id"]] as $sub1_mn)
		{
			$ret.="<option value=$sub1_mn[id] data-level='1'".($sub1_mn["id"]==$selected?"selected=''":"")." ".(($type!="" && !strpos("a".$sub1_mn["type"],$type))?"disabled=''":"").">$sub1_mn[name]</option>\n";
			if(count($menu[$sub1_mn["id"]])>0)
			foreach($menu[$sub1_mn["id"]] as $sub2_mn)
			{
				$ret.="<option value=$sub2_mn[id] data-level='2'".($sub2_mn["id"]==$selected?"selected=''":"")." ".(($type!="" && !strpos("a".$sub2_mn["type"],$type))?"disabled=''":"").">$sub2_mn[name]</option>\n";
			}
		}
	}
	$ret.="</select>";
	$ret.="<script>
			 function format(state) {
				var originalOption = state.element;
				var level=$(originalOption).data('level');
				if(level=='0')
					return \"<img class='flag' src='./admin/img/folder.png' alt='' />\" + state.text;
				if(level=='1')
					return \"<img class='flag' src='./admin/img/ico_page.png' alt='' / style='margin-left:30px;'>\" + state.text;
					
			}
			$(\"#selectmn\").select2({
				formatResult: format,
				formatSelection: format,
				escapeMarkup: function(m) { return m; }
			});
			</script>";
	return $ret;
}

function getlistRound($name,$selected="")
{
	$q = query("select * from round");
	while($r=fetch($q))
	{
		$menu[]=$r;
	}
	$ret="<select name='$name' id='selectround' style='width:200px;'>";
	$ret.="";
	if(count($menu)>0)
	foreach($menu as $mn)
	{

		$ret.="<option value=$mn[id] ".($mn["id"]==$selected?"selected=''":"")." data-level='0' >$mn[roundname]</option>\n";
	}
	$ret.="</select>";
	$ret.="<script>
			 function formatRound(state) {
				var originalOption = state.element;
				var level=$(originalOption).data('level');
					return state.text;
					
			}
			$(\"#selectround\").select2({
				formatResult: formatRound,
				formatSelection: formatRound,
				escapeMarkup: function(m) { return m; }
			});
			</script>";
	return $ret;
}

function getlistMember($name,$selected="")
{
	$q = query("select id,name from member");
	while($r=fetch($q))
	{
		$menu[]=$r;
	}
	$ret="<select name='$name' id='selectmember' style='width:200px;'>";
	if(count($menu)>0)
	foreach($menu as $mn)
	{
		$ret.="<option value=$mn[id] ".($mn["id"]==$selected?"selected=''":"")." data-level='0' >$mn[name]</option>\n";
	}
	$ret.="</select>";
	$ret.="<script>
			 function formatMember(state) {
				var originalOption = state.element;
				var level=$(originalOption).data('level');
					return state.text;
					
			}
			$(\"#selectmember\").select2({
				formatResult: formatMember,
				formatSelection: formatMember,
				escapeMarkup: function(m) { return m; }
			});
			</script>";
	return $ret;
}
?>